#!/usr/bin/env python

import gtk
from signals import fileSelectionDialogSignals
import gettext
gettext.install('blendIT','locale');

class fileSelectionDialog(gtk.FileChooserDialog):

   filters = [
      (_("Image Files"),("image/png","image/jpeg","image/svg+xml",
                        "image/svgz+xml","image/gif"),()),
      (_("Audio Files"),("audio/wav","audio/midi","audio/aac","audio/mp3",
                         "audio/amr","audio/amr-wb","audio/mp4","audio/au",
                         "application/ogg","audio/flac"),()),
      (_("MXit Files"),("application/mxit-msgs","application/mxit-skin",
                        "application/mxit-emo","application/mxit-emof"),
                       ("*.mxf","*.mxm")),
      (_("All Files"),(),("*"))
      ];

   def __init__(self,blend,title,signal,parent=None,
                method=gtk.FILE_CHOOSER_ACTION_OPEN,params=None):

      gtk.FileChooserDialog.__init__(self,title,parent,method);

      self.sigs = fileSelectionDialogSignals.signals(blend);

      for i in range(0,len(self.filters)):
         filter = gtk.FileFilter();
         filter.set_name(self.filters[i][0]);
         for n in range(0,len(self.filters[i][1])):
            filter.add_mime_type(self.filters[i][1][n]);
         for n in range(0,len(self.filters[i][2])):
            filter.add_pattern(self.filters[i][2][n]);
         self.add_filter(filter);

      self.add_button(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL);
         
      if method == gtk.FILE_CHOOSER_ACTION_OPEN:
         parms = self.openDialog();
         self.add_button(gtk.STOCK_OPEN,gtk.RESPONSE_ACCEPT);
         self.connect("response",signal,params,parms);
      else:
         parms = None;
         self.add_button(gtk.STOCK_SAVE,gtk.RESPONSE_ACCEPT);
         self.connect("response",signal,params,parms);
      

      self.set_property('do-overwrite-confirmation',True);
      self.connect("close",self.sigs.on_close);
      self.set_border_width(4);
      
   def openDialog(self):
      # Implement the preview widget for images
      imgWidget = gtk.Image();
      imgWidget.show();
      self.set_preview_widget(imgWidget);
      self.set_use_preview_label(False);

      self.connect('selection-changed',self.sigs.on_selection_change);
         
      # Also add a description text entry widget. It'll come in useful
      entry = gtk.Entry();
      label = gtk.Label(_("Description: "));
      hbox = gtk.HBox();
      hbox.pack_start(label,False,False);
      hbox.pack_end(entry,True,True);
      hbox.set_spacing(6);
      entry.show();
      label.show();
      hbox.show();
      self.set_extra_widget(hbox);

      return entry;